<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <div class="seach-box">
        <div class="select-box-content">
          <div class="select-box-content-name">故障类型：</div>
          <el-select
            clearable
            v-model="hitchTypeName"
            placeholder="请选择"
            @change="queryPageList()"
          >
            <el-option
              v-for="item in hitchTypeArr"
              :key="item.hitchTypeCode"
              :label="item.hitchTypeName"
              :value="item.hitchTypeCode"
            ></el-option>
          </el-select>
        </div>
      </div>
    </template>
    <template v-slot:btnsBar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :page-no="147"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:content>
      <e-table
        ref="tableData"
        rowKey="hitchTypeId"
        :table-data="tableData"
        :table-columns="tableLabel"
        :page="currentPage"
        :size="pageSize"
        :total="totalSize"
        :loading="loading"
        :table-option="tableOption"
        @sizeChange="changePageSize"
        @pageChange="changeCurrentPage"
        @handleSelectionChange="handleSelectionChange"
        @tableUpdatePlus="mixTableUpdatePlus"
      >
        <template
          slot="operate"
          slot-scope="scoped"
          v-if="scoped.row.hitchTypeCode"
        >
          <span>
            <span
              class="table-btns-box"
              @click="edit(scoped.row)"
              v-if="judgePermission(147,'edit')"
            >
              <i class="el-icon-edit-outline"></i>
              <span>修改</span>
            </span>
            <span
              :class="noDelList.findIndex(a => a === scoped.row.hitchTypeCode)===-1?'table-btns-box table_del_btn':'cannot'"
              @click="delOne(scoped.row)"
              v-if="judgePermission(147,'deleteone')"
            >
              <i class="el-icon-delete"></i>
              <span>删除</span>
            </span>
          </span>
        </template>
      </e-table>
      <AddEditDialog
        ref="addEditDialog"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import repairTypeApi from "@/api/equip-repair-manage/repair-type";
import BaseMainSlot from "@/components/BaseMainSlot/kong-new.vue";
import AddEditDialog from "./components/add-edit-dialog.vue";
import { mymixin } from "@/utils/mixin";
import { mapGetters } from "vuex";

export default {
  name: "RepairType",
  components: { ActionBar, BaseMainSlot, AddEditDialog },
  mixins: [mymixin],
  data() {
    return {
      // 所搜条件：类型名称
      hitchTypeName: "",
      checkedIdList: [],
      noDelList: [],
      // 表单展示项
      tableLabel: [
        {
          label: "故障类型名称",
          param: "hitchTypeName", // 表格显示参数
          show: true,
          notSearch: true,
          selectValue: "hitchTypeCode", // Select 实际查询参数
          // type: "Select", // input Select Checkbox date
          // typeObj: {
          //   // 动态数据和静态数据
          //   list: [],
          //   listName: ["hitchTypeName", "hitchTypeCode"], // 动态数据和静态数据数组字段名
          // },
        },
        {
          label: "备注",
          param: "description",
          show: true,
          notSearch: true,
        },
      ],

      // 表单事件
      tableOption: {
        label: "操作",
        options: [
          {
            label: "操作",
            type: "text",
            icon: "el-icon-edit-outline",
            methods: "operate",
            slotName: "operate",
          },
        ],
      },
    };
  },
  computed: {
    ...mapGetters({
      judgePermission: "permission/judgePermission",
    }),
  },
  watch: {},
  created() {
    this.queryhitchTypeList();
    this.queryPageList();
    // 查询已经被启用的类型（不能删除）
    this.queryQuotedEquHitchType();
  },
  mounted() {},
  methods: {
    // 条件查询故障类型
    queryhitchTypeList() {
      this.hitchTypeArr = [];
      repairTypeApi.findAll({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.hitchTypeArr = data;
        }
      });
    },
    queryPageList(e) {
      this.loading = false;
      // 获取数据
      let param = {
        page: this.currentPage,
        size: this.pageSize,
        entity: {
          hitchTypeCode: this.hitchTypeName || e?.hitchTypeName,
        },
      };
      repairTypeApi.queryPageList(param).then((res) => {
        this.loading = false;
        if (res.code === 1) {
          let data = res.data;
          this.totalSize = data.totalElements;
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        }
      });
    },

    selectAll(list) {
      let notvalue = true;
      let notvalue1 = true;
      list.forEach((item) => {
        var index = this.noDelList.findIndex((a) => a === item.hitchTypeCode);
        if (index === -1) {
          if (!item.hitchTypeCode) {
            notvalue = false;
          }
        } else {
          notvalue1 = false;
        }
      });
      if (notvalue === false && notvalue1 === true) {
        this.$message.warning("不可选择空数据！");
      } else if (notvalue1 === false && notvalue === true) {
        this.$message.warning("类型已被使用，请勿删除！");
      } else if (notvalue1 === false && notvalue === false) {
        this.$message.warning("类型已被使用，请勿删除！，不可选择空数据！");
      }
    },
    select(list) {
      let notvalue = true;
      let notvalue1 = true;
      list.forEach((item) => {
        var index = this.noDelList.findIndex((a) => a === item.hitchTypeCode);
        if (index === -1) {
          if (!item.hitchTypeCode) {
            notvalue = false;
          }
        } else {
          notvalue1 = false;
        }
      });
      if (notvalue === false && notvalue1 === true) {
        this.$message.warning("不可选择空数据！");
      } else if (notvalue1 === false && notvalue === true) {
        this.$message.warning("类型已被使用，请勿删除！");
      } else if (notvalue1 === false && notvalue === false) {
        this.$message.warning("类型已被使用，请勿删除！，不可选择空数据！");
      }
    },
    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      let emptyList = [];
      this.checkedIdList = list.map((item) => {
        var index = this.noDelList.findIndex((a) => a === item.hitchTypeCode);
        if (index === -1) {
          if (item.hitchTypeCode) {
            return item.hitchTypeCode;
          } else {
            notvalue = false;
            emptyList.push(item);
            return "";
          }
        } else {
          this.$message.warning("类型已被使用，不可删除！");
          this.toggleNoSelection([item]);
          return;
        }
      });
      if (notvalue === false) {
        // this.$message.warning("不可选择空数据！");
        this.toggleNoSelection(emptyList);
        return;
      }
      if (this.checkedIdList.length >= 1) this.delActive = true;
      else this.delActive = false;
    },
    // 查询已经被启用的类型（不能删除）
    queryQuotedEquHitchType() {
      repairTypeApi.queryQuotedEquHitchType({}).then((res) => {
        if (res.code === 1) {
          let data = res.data || [];
          this.noDelList = data;
        }
      });
    },
    // 单个删除
    delOne(data) {
      var index = this.noDelList.findIndex((a) => a === data.hitchTypeCode);
      if (index === -1) {
        this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          customClass: "myClass",
        })
          .then(() => {
            var form = new FormData();
            form.append("hitchTypeCodeList", data.hitchTypeCode);
            repairTypeApi.delList(form).then((res) => {
              if (res.code === 1) {
                this.$message.success(res.msg);
                this.queryPageList();
                this.resetActionBar();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    // 表格删除
    delmessage() {
      var form = new FormData();
      this.checkedIdList.forEach((a) => {
        form.append("hitchTypeCodeList", a);
      });
      repairTypeApi.delList(form).then((res) => {
        if (res.code === 1) {
          this.$message.success(res.msg);
          this.queryPageList();
          this.resetActionBar();
        }
      });
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.editActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.hitchTypeCode) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
  },
};
</script>

<style lang="scss" scoped>
@import "~@/styles/variables.scss";

::v-deep .el-collapse-item__header {
  height: 0px;
}
</style>
